<?php

//PLUGIN type=action
//PLUGIN name=page.save
//PLUGIN desc=Enables you to save page information
//PLUGIN group=native

    $c_xml->meta->title = $_POST['title'];
    $c_xml->meta->description = $_POST['description'];
    $c_xml->meta->menuname = $_POST['menuname'];
    $c_xml->meta->show = $_POST['show'];

    // custom page properties
    $c_xml->meta->custom = null;

    if (isset($_POST['custom']))
        foreach ($_POST['custom'] as $k => $c)
        {
            $c_xml->meta->custom->$k = $c;
        }

    // sort subpages
    if (!empty($_POST['subpages_sort']))
    {
        // for each subpage, open the subpage' xml file and define its sort order.
        foreach ($_POST['subpages_sort'] as $k => $i)
        {
            $cwp = _CONTENTPATH_ . $i . "/index.xml";
            if (file_exists($cwp))
            {
                if ($temp_xml = simplexml_load_file($cwp))
                {
                    $temp_xml->meta->sort = $k;
                    if (!file_put_contents($cwp, $temp_xml->asXML()))
                    {
                        exit("XML error - failed to save XML after sorting");
                    }
                }
            }
        }
    }
    if (!file_put_contents($c_xml_file, $c_xml->asXML()))
    {
        $msg = "Error saving XML";
    }
    else
    {
        if ($c_xml = simplexml_load_file($c_xml_file))
        {
            $newName = false;
            if ($_POST['urlname'] != $options['urlname'] && file_exists(_CONTENTPATH_ . $options['page']) && strlen($options['page']) > 1 && strlen($_POST['urlname']) > 1)
            { // move page here
                $a = explode("/", $options['page']);
                $e = array_pop($a);
                $from = _CONTENTPATH_ . $options['page'];
                $newName = implode("/", $a) . "/" . secureUrl($_POST['urlname']);

                if (!move($from, _CONTENTPATH_ . $newName))
                    exit("Move action failed!");
            }
            if (!empty($_REQUEST['nq']))
            {
                header("Location:?q=" . $_REQUEST['nq']);
                exit("failure");
            }
            if ($newName)
            {
                header("Location:?q=command:cms;view:cms.page.settings;page:" . $newName);
                exit();
            }
            header("Location:" . $_SERVER['REQUEST_URI']);
            $msg = "Page settings saved. refresh page to view changes.";
        }
        else
        {
            $msg = "Error reloading XML";
        }
    }
    $smarty->assign("msg", $msg);

?>